﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.HealthCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.HealthCommandHandler
{
    public class UpdateHealthCommandHandler:IRequestHandler<UpdateHealthCommand,APIResult<int>>
    {
        private readonly IBaseRepository<SU_Health> healthRep;
        public UpdateHealthCommandHandler(IBaseRepository<SU_Health> healthRep)
        {
            this.healthRep = healthRep;
        }
        public async Task<APIResult<int>> Handle(UpdateHealthCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list = healthRep.GetAll().Result.Where(x => x.HealthId == request.HealthId).FirstOrDefault();
            list.HealthId=request.HealthId;
            list.BreedDay = request.BreedDay;
            list.HealthType = request.HealthType;
            list.HealthMethod = request.HealthMethod;
            var res = healthRep.Update(list).Result;
            if (res > 0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
                result.data = res;
            }
            return await Task.FromResult(result);
        }
    }
}
